package org.opentcs.kcvehicle.communication.kc.udp.agv.param.rsp;


import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEventHeader;

public class RcvEventPackage {

  private boolean isOk;
  private byte[] value;
  private String content;
  // header size = 授权码16 + 报文头12(包括报文数据长度2个字节+保留2个字节) = 28
  private AgvEventHeader header;
  private byte[] dataBytes;


  private String getContent(byte i) {
    if (i == 0x00) {
      return "成功执行";
    }
    else if (i == 0x01) {
      return "执行失败，原因未知";
    }
    else if (i == 0x02) {
      return "服务码错误";
    }
    else if (i == 0x03) {
      return "命令码错误";
    }
    else if (i == 0x04) {
      return "报文头部错误";
    }
    else if (i == 0x80) {
      return "无法执行命令，因为当前车辆导航状态与命令冲突";
    }
    else if (i == 0xFF) {
      return "协议授权码错误";
    }
    else {
      return "未知错误";
    }

  }

  public RcvEventPackage(byte executionCode, byte[] value) {
    if (executionCode == 0x00) {
      this.isOk = true;
      this.header = new AgvEventHeader(ByteUtils.copyBytes(value, 0, 28));
      this.dataBytes = ByteUtils.copyBytes(value, 28, value.length - 28);
    }
    else {
      this.isOk = false;
    }
    this.value = value;
    this.content = getContent(executionCode);
  }

  public RcvEventPackage() {
  }

  public boolean isOk() {
    return isOk;
  }

  public void setValue(byte[] value) {
    this.value = value;
  }

  public byte[] getValue() {
    return value;
  }

  public String getContent() {
    return content;
  }

  public AgvEventHeader getHeader() {
    return header;
  }

  public byte[] getDataBytes() {
    return dataBytes;
  }

  @Override
  public String toString() {
    return "RcvPackage{" +
        "isOk=" + isOk +
        ", value=" + value +
        ", content='" + content + '\'' +
        '}';
  }
}
